<?php
/**
 * Created by Command.
 * User: Vito
 */

namespace app\admin\curd\check_in;

use app\common\curd\Index;
use think\db\Query;
class CheckInIndex extends Index
{
    /**
     * 关键词查询指定字段
     * @var string[]
     */
    protected $keywordQueryArgs = [];
    //追加标签数据
    protected $labelCallback = [
        // 'get_status_enum' => ['name' => '状态'],
    ];
    //查询条件
    protected $where = [];
    //字段
    protected $field = [];
    //排除字段
    protected $withoutField = false;
    //关联
    protected $with = [
        // 'roomType', 'payRecord' ,'building',
        'user', 'room', 'payRecord'
    ];

    protected $order = ['id'=>'desc'];
    /**
     * 需要查询条件的字段
     * 数据库字段和参数字段名不一样 'db_field' => 'param_field'
     * 数据库字段和参数字段名一样 'field'
     * @var array
     */
    protected $queryField = [

    ];
    protected $middleware = [
        'middleWhere'
    ];

    protected function middleWhere($next)
    {
        $kw = $this->request['kw'];
        $status = $this->request['filter'];
        if($kw){
            $this->field = [
                'RoomLeaseRecord.id'=> 'id',
                'RoomLeaseRecord.*'
            ];
        }
        // halt($status['status']);
        if($status['status'] ==2){
            $this->order = ['end_time'=>'desc'];
        }
        return $next();
    }
    protected function queryCallback(Query $query)
    {
        // halt($this->request);
        $kw = $this->request['kw'];
        if ($kw) {
            $query
            ->join('wuye_room Room', 'Room.id = RoomLeaseRecord.room_id', 'right')
            ->join('wuye_user user', 'user.id = RoomLeaseRecord.user_id', 'right')
            ->join('wuye_building building', 'building.id = Room.building_number', 'right')
            ->where('Room.name|user.real_name|user.tel|user.word_unit|building.name|building.building_number', 'like', '%' . $kw . '%');
        }
    }
    // protected function middleWhere($next)
    // {
    //     $kw = $this->request['kw'];
    //     // halt($kw);
    //     if ($kw) {
    //         $this->field =
    //             [
    //                 'Building.id' => 'building_id',
    //                 'RoomType.id' => 'room_type_id',
    //                 'Room.building_number' => 'building_number',
    //                 'Room.*'
    //                 // 'payRecord.*'
    //             ];
    //     }
    //     // if ($type == 'valid') {
    //     //     $this->field =
    //     //         [
    //     //             'Building.id' => 'building_id',
    //     //             'RoomType.id' => 'room_type_id',
    //     //             'Room.building_number' => 'building_number',
    //     //             'Room.*'
    //     //         ];
    //     // }

    //     return $next();
    // }
    // protected function queryCallback(Query $query)
    // {
    //     // halt($this->request);
    //     $kw = $this->request['kw'];
    //     if ($kw) {
    //         $value = str_split($kw);
    //         // halt($value[count($value)-3]. $value[count($value) - 2]. $value[count($value) - 1]);
    //         $kws = $value[count($value) - 3] . $value[count($value) - 2] . $value[count($value) - 1];
    //         $query
    //             ->join('wuye_room_type RoomType', 'RoomType.id = Room.room_type', 'right')
    //             ->join('wuye_building Building', 'Building.id = Room.building_number', 'right')
    //         // ->join('wuye_pay_record pr', 'pr.room_id = Room.id', 'right')
    //         ->where('Room.name', 'like', '%' . $kws . '%');
    //         // ->whereOr('Building.name', 'like', '%' . $kw . '%')
    //         // ->whereOr('Room.name', 'like', '%' . $Room_name .'%')
    //         // ->whereOr('Building.building_number', 'like', '%' . $kw . '%')
    //         // ->whereOr('RoomType.name', 'like', '%' . $kw . '%');
    //     }
    //     // if ($type == 'valid') {
    //     //     // $where['RoomType.live_number'] = ['>', 'Room.use_people'];
    //     //     $query
    //     //         ->join('wuye_room_type RoomType', 'RoomType.id = Room.room_type', 'right')
    //     //         ->join('wuye_building Building', 'Building.id = Room.building_number', 'right')
    //     //         ->where('RoomType.live_number > Room.use_people');
    //     // }
    // }
}